package com.yc.cangst.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.cangst.mapper.EbProductMapper;
import com.yc.cangst.model.EbProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EbProductServiceImpl implements EbProductService{

    @Autowired
    private EbProductMapper ebProductMapper;

    @Override
    public Page<EbProduct> selectList(Integer psize, Integer pageSize) {

        Page<EbProduct> page = new Page<>(psize, pageSize);

        Page<EbProduct> productPage = ebProductMapper.selectPage(page, null);

        return productPage;
    }

    @Override
    public EbProduct detail(Integer epId) {
        return ebProductMapper.selectById(epId);
    }

    @Override
    public List<EbProduct> selectByCateId(Integer cateId, Integer pSize, Integer pageSize) {
        Page<EbProduct> page=  new Page();
        Page<EbProduct> ebProductList = ebProductMapper.selectPage(page, Wrappers.<EbProduct>lambdaQuery()
                .eq(EbProduct::getEpcChildId, cateId));

        return ebProductList.getRecords();
    }

    @Override
    public int getMaxPage(Integer pageSize) {

        int rows = ebProductMapper.selectCount(null);

        int maxPage =(int)Math.ceil((rows*1.0)/pageSize);
        return maxPage;
    }

    @Override
    public List<EbProduct> selectByCondition(String searchText) {

        return ebProductMapper.selectList(Wrappers.<EbProduct>lambdaQuery().like(EbProduct::getEpName,searchText));

    }

}
